#include "objective_function.hh"

objective_function::objective_function(direction dir_)
  : dir(dir_)
{
}
objective_function::objective_function()
{
}

int objective_function::get_dir() const
{
  switch (dir)
    {
    case MAX:
      return GLP_MAX;
    case MIN:
      return GLP_MIN;
    };
  return GLP_MIN;
}

double objective_function::get_coeff(const variable& v) const
{
  std::vector<std::pair<variable, double> >::const_iterator it;

  it = variables.begin();

  while ((it != variables.end()) && (v != it->first))
    {
      ++it;
    }

  if (it != variables.end())
    return it->second;
  else
    return 0;
}

void
objective_function::add_variable (const variable& var, double coeff)
{
  variables.push_back (std::pair<variable, double> (var, coeff));
}


std::ostream& operator << (std::ostream& stream, const objective_function& w)
{
  if (w.dir == objective_function::MAX)
    stream << "Max z = ";
  else
    stream << "Min z = ";

  std::vector<std::pair<variable, double> >::const_iterator it;

  it = w.variables.begin();

  while (it != w.variables.end())
    {
      stream << it->second << " * " << it->first;
      ++it;
      if (it != w.variables.end())
	stream << " + ";
    }
  return stream;
}
